﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SpecListSPC;
using CADTools;
using System.IO;

namespace ArchSoft
{
    public partial class LayerManager : Form
    {

        int c = 0;

        public LayerManager()
        {
            InitializeComponent();
        }

        private string StoreFol_ = "";
        public string StoreFol
        {
            set { StoreFol_ = value; }
        }

        //Insert Item!
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
                ColorCell ccel = new ColorCell();
                ccel.ColorString = "White";
                ccel.Color = Color.White;
                ccel.ColorValue = 7;
                ccel.ColorSelecting += new ColorCell.ColorSelectingHandle(ccel_ColorSelecting);
                ccel.CellDoubleClick += new ColorCell.CellDoubleClickHandle(ccel_CellDoubleClick);

                GLItem nit = new GLItem();
                GLSubItem Sit1 = new GLSubItem();
                Sit1.Text = "A_New Layer" + c;


                GLSubItem Sit2 = new GLSubItem();
                Sit2.Control = ccel;

                GLSubItem Sit3 = new GLSubItem();
                Sit3.Text = "Continuous";

                nit.SubItems.AddRange(new GLSubItem[] { Sit1, Sit2, Sit3 });


                this.specList2.Items.Add(nit);
                nit.Selected = true;
                this.specList2.Refresh();
                this.specList2.ActivateEmbeddedControl(0, nit, nit.SubItems[0]);
                c++;
            }
            catch { }
        }

        void ccel_CellDoubleClick(object sender, EventArgs e)
        {
            ACIChooser fr = new ACIChooser(((ColorCell)sender).ColorValue);
            fr.StartPosition = FormStartPosition.CenterScreen;
            if (fr.ShowDialog() == DialogResult.OK)
            {
                ((ColorCell)sender).ColorValue = fr.ColorIdex;
                ((ColorCell)sender).Color = fr.RealColor;
                ((ColorCell)sender).ColorString = fr.ColorName;
            }
        }

        void ccel_ColorSelecting(object sender, EventArgs e)
        {
            ACIChooser fr = new ACIChooser(((ColorCell)sender).ColorValue);
            fr.StartPosition = FormStartPosition.CenterScreen;
            if (fr.ShowDialog() == DialogResult.OK)
            {
                ((ColorCell)sender).ColorValue = fr.ColorIdex;
                ((ColorCell)sender).Color = fr.RealColor;
                ((ColorCell)sender).ColorString = fr.ColorName;
            }
        }

        private void specList2_KeyDown(object sender, KeyEventArgs e)
        {
            if (specList2.SelectedItems.Count == 0)
                return;
            //Rename
            if (e.KeyData == Keys.F2)
            {
                GLItem it = (GLItem)this.specList2.SelectedItems[0];
                this.specList2.ActivateEmbeddedControl(0, it, it.SubItems[0]);
            }
            //Insert!
            if (e.KeyData == Keys.Insert)
            {
                toolStripButton3_Click(sender, e);
            }
            //delete!
            if (e.KeyData == Keys.Delete)
            {
                try
                {
                    if (this.specList2.SelectedItems.Count > 0)
                        this.specList2.Items.RemoveAt(this.specList2.HotItemIndex);
                    this.specList2.Refresh();
                }
                catch { }
            }
            //Insert!
            if (e.KeyData == Keys.Enter)
            {
                toolStripButton3_Click(sender, e);
            }
        }

        //Delete!
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.specList2.SelectedItems.Count > 0)
                    this.specList2.Items.Remove((GLItem)this.specList2.SelectedItems[0]);
                this.specList2.Refresh();
            }
            catch { }
        }

        //Insert
        private void inSertNewLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton3_Click(sender, e);
        }

        //Delete!
        private void deleteNewLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButton4_Click(sender, e);
        }

        //Rename!
        private void renameLayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (specList2.SelectedItems.Count == 0)
                return;
            GLItem it = (GLItem)this.specList2.SelectedItems[0];
            this.specList2.ActivateEmbeddedControl(0, it, it.SubItems[0]);
        }

        //Save!
        private void button1_Click_1(object sender, EventArgs e)
        {
            //pro
            try
            {
                if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + Path.DirectorySeparatorChar + ProjectManager.LAYER_STORE_NAME))
                    File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + Path.DirectorySeparatorChar + ProjectManager.LAYER_STORE_NAME);

                StreamWriter srt = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + Path.DirectorySeparatorChar + ProjectManager.LAYER_STORE_NAME);
                srt.WriteLine(EncodeDecode.GetEncoded(this.specList2.Items.Count.ToString()));
                foreach (GLItem it in this.specList2.Items)
                {
                    if (it.SubItems[0].Text.IndexOf("A_") >= 0)
                        srt.WriteLine(EncodeDecode.GetEncoded(it.SubItems[0].Text));
                    else srt.WriteLine(EncodeDecode.GetEncoded("A_" + it.SubItems[0].Text));
                    srt.WriteLine(EncodeDecode.GetEncoded(((ColorCell)it.SubItems[1].Control).ColorString));
                    srt.WriteLine(EncodeDecode.GetEncoded(((ColorCell)it.SubItems[1].Control).ColorValue.ToString()));
                    Color cl = ((ColorCell)it.SubItems[1].Control).Color;
                    srt.WriteLine(EncodeDecode.GetEncoded(cl.R + "," + cl.G + "," + cl.B));
                    srt.WriteLine(EncodeDecode.GetEncoded("Type: " + it.SubItems[2].Text));
                }
                srt.Close();
            }
            catch { }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        void LoadLayers()
        {
            try
            {
                specList2.Items.Clear();
                StreamReader srt = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + Path.DirectorySeparatorChar + ProjectManager.LAYER_STORE_NAME);
                string crix = "";
                crix = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });
                int t = int.Parse(crix);

                for (int i = 0; i < t; i++)
                {

                    ColorCell ccel = new ColorCell();
                    ccel.ColorSelecting += new ColorCell.ColorSelectingHandle(ccel_ColorSelecting);
                    ccel.CellDoubleClick += new ColorCell.CellDoubleClickHandle(ccel_CellDoubleClick);



                    GLItem nit = new GLItem();
                    GLSubItem Sit1 = new GLSubItem();
                    Sit1.Text = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });
                    //Sit1.ChangedEvent += new ChangedEventHandler(Sit1_ChangedEvent);
                    //Sit1.Control = LayerNameLable;
                    //LayerNameLable.Text = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });




                    crix = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });
                    ccel.ColorString = crix;
                    ccel.ColorValue = int.Parse(EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' }));
                    crix = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });
                    ccel.Color = Color.FromArgb(int.Parse(crix.Split(',')[0]), int.Parse(crix.Split(',')[1]), int.Parse(crix.Split(',')[2]));
                    GLSubItem Sit2 = new GLSubItem();
                    Sit2.Control = ccel;

                    GLSubItem Sit3 = new GLSubItem();
                    Sit3.Text = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' }).Split(':')[1].Trim();
                    nit.SubItems.AddRange(new GLSubItem[] { Sit1, Sit2, Sit3 });

                    this.specList2.Items.Add(nit);

                    this.specList2.Refresh();
                }
                srt.Close();
            }
            catch { }
        }

        private void LayerManager_Load(object sender, EventArgs e)
        {

            if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + Path.DirectorySeparatorChar + ProjectManager.LAYER_STORE_NAME))
            {
                return;
            }
            LoadLayers();
        }
        void Insert2List()
        {

        }

        private void specList2_SizeChanged(object sender, EventArgs e)
        {
            if (this.specList2.Columns[0].Width + this.specList2.Columns[1].Width < this.specList2.Width - 30)
                this.specList2.Columns[2].Width = specList2.Width - this.specList2.Columns[0].Width - this.specList2.Columns[1].Width - 20;
        }

        //Import existed file!
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog of = new OpenFileDialog();
                of.Title = "Import Layer Define File.";
                of.Filter = "Layers Information File|*.LDI| All files|*.*";
                of.Multiselect = false;
                if (of.ShowDialog() != DialogResult.OK)
                    return;

                specList2.Items.Clear();
                StreamReader srt = new StreamReader(of.FileName);
                string crix = "";
                crix = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });
                int t = int.Parse(crix);

                for (int i = 0; i < t; i++)
                {
                    ColorCell ccel = new ColorCell();
                    ccel.ColorSelecting += new ColorCell.ColorSelectingHandle(ccel_ColorSelecting);
                    ccel.CellDoubleClick += new ColorCell.CellDoubleClickHandle(ccel_CellDoubleClick);

                    GLItem nit = new GLItem();
                    GLSubItem Sit1 = new GLSubItem();
                    Sit1.Text = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });

                    crix = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });
                    ccel.ColorString = crix;
                    ccel.ColorValue = int.Parse(EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' }));
                    crix = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' });
                    ccel.Color = Color.FromArgb(int.Parse(crix.Split(',')[0]), int.Parse(crix.Split(',')[1]), int.Parse(crix.Split(',')[2]));
                    GLSubItem Sit2 = new GLSubItem();
                    Sit2.Control = ccel;

                    GLSubItem Sit3 = new GLSubItem();
                    Sit3.Text = EncodeDecode.GetDecoded(srt.ReadLine()).TrimEnd(new char[] { '\0' }).Split(':')[1].Trim();
                    nit.SubItems.AddRange(new GLSubItem[] { Sit1, Sit2, Sit3 });

                    this.specList2.Items.Add(nit);

                    this.specList2.Refresh();
                }
                srt.Close();
            }
            catch
            {
                //MessageBox.Show("Import fail. Wrong Format File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); LoadLayers();
                MessageBox.Show("Nhập thất bại . Tạp tin định dạng sai", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error); LoadLayers();
            }
        }

        //Export Layer Manager file!
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (this.specList2.Items.Count == 0)
            {
                //MessageBox.Show("You can not export file because list of layers is null.", "Note", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                MessageBox.Show("Bạn không thể xuất tập tin, vì danh sách các lớp rỗng .", "Ghi chú", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                string bf = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + Path.DirectorySeparatorChar + ProjectManager.LAYER_STORE_NAME;
                if (!File.Exists(bf))
                {
                    //Chua co nen fai tao lai file moi.               

                    StreamWriter srt = new StreamWriter(bf);
                    srt.WriteLine(this.specList2.Items.Count);
                    foreach (GLItem it in this.specList2.Items)
                    {
                        srt.WriteLine(it.SubItems[0].Text);
                        srt.WriteLine(((ColorCell)it.SubItems[1].Control).ColorString);
                        srt.WriteLine(((ColorCell)it.SubItems[1].Control).ColorValue);
                        Color cl = ((ColorCell)it.SubItems[1].Control).Color;
                        srt.WriteLine(cl.R + "," + cl.G + "," + cl.B);
                        srt.WriteLine("Type: " + it.SubItems[2].Text);
                    }
                    srt.Close();
                }

                SaveFileDialog sf = new SaveFileDialog();
                sf.Title = "Export To Layer Define File.";
                sf.Filter = "Layers Information File|*.LDI";
                if (sf.ShowDialog() == DialogResult.OK)
                {
                    File.Copy(bf, sf.FileName);
                }
            }
            catch { }
        }

        private void helpToolStripButton1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://trang.com.vn/?frame=support");
        }
    }
}